﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Mysoft.Platform.Component.Attributes;
using Mysoft.Platform.Component.Caches;
using Mysoft.Platform.OnlineLibrary.BaseControllers;

namespace Mysoft.Platform.OnlineLibrary.Controllers
{
	[Auth]
	public class MenuController : BaseController
	{
		//
		// GET: /Menu/

		public JsonResult Index()
		{
			// or read from database and store in cache.
			if( CacheManager.Default.Get<JsonResult>(CacheManager.GetMenuKey(MyGroup)) == null ) {
				CacheManager.Default.Insert(CacheManager.GetMenuKey(MyGroup), Json(GetMenu(MyGroup), JsonRequestBehavior.AllowGet), DateTime.Now.AddDays(-1), TimeSpan.FromSeconds(0));
			}
			return CacheManager.Default.Get<JsonResult>(CacheManager.GetMenuKey(MyGroup));
		}

		private List<Menu> GetMenu(UserGroup group = UserGroup.User)
		{
			List<Menu> menus = new List<Menu>();

			Menu home = new Menu() { Id = "Home", Hidden = false, Link = Url.Action("Index", "Home"), Name = "图书馆" };
			Menu history = new Menu() { Id = "History", Hidden = false, Link = Url.Action("Index", "History"), Name = "已借图书/续借" };
			Menu batchLog = new Menu() { Id = "BatchLog", Hidden = false, Link = Url.Action("Index", "BatchLog"), Name = "操作记录" };
			Menu lendedBooks = new Menu() { Id = "LendedBooks", Hidden = false, Link = Url.Action("Index", "LendedBooks"), Name = "借阅管理" };

			menus.Add(home);
			menus.Add(history);
			menus.Add(batchLog);
			menus.Add(lendedBooks);

			if( group == UserGroup.Admin ) {
				Menu bookManage = new Menu() { Id = "BookManage", Hidden = false, Link = Url.Action("Index", "BookManage"), Name = "图书管理" };
				menus.Add(bookManage);
			}

			return menus;
		}
	}

	public class Menu
	{
		public string Name { get; set; }

		public string Link { get; set; }

		public string Id { get; set; }

		public bool Hidden { get; set; }
	}
}